<?php
/**
 * Show and manage the post feeds
 *
 * @author University of London Computer Centre
 * @version 1.3.0 - Release Candidate 1
 * @package ArchivePress
 * @subpackage pages
 * @global ArchivePress $ArchivePress
 * @global ArchivePressLog $ArchivePressLog
 */

// globals
global $ArchivePress;
global $ArchivePressLog;

// actions URL
$responder = get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/responder.php&amp;back=edit-posts';


// get info to apply the filters
// STATUS (all or only active feeds)
$status = "";
if(isset($_GET["post_status"])){
    $status = attribute_escape($_GET["post_status"]);
}

// SEARCH FIELD
$search = "";
if(isset($_GET["s"])){
    $search = stripslashes($_GET["s"]);
}

// PROTOCOL LIST
$protocol = "";
if(isset($_GET["protocol"])){
    $protocol = attribute_escape($_GET["protocol"]);
    if($protocol == "ALL"){
        $protocol = "";
    }
}

// CHECK FOR MESSAGES
$message = "";
// active
if(array_key_exists("active", $_GET)){
    if((int)$_GET["active"] == 1){
        // SINGLE ACTION
        $message = sprintf(__("Feed Activate. <a href='%s'>Undo</a>",ARCHIVEPRESS), wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=deactivatefeed&amp;feed_id='.(int)$_GET["feed_id"], WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_postfeed_' . (int)$_GET["feed_id"]));
    } else {
        // BULK ACTIONS
        $message = sprintf(__("%d Feeds Activate",ARCHIVEPRESS), (int)$_GET["active"]);
    }
}

// inactive
if(array_key_exists("inactive", $_GET)){
    if((int)$_GET["inactive"] == 1){
        // SINGLE ACTION
        $message = sprintf(__("Feed Deactivate. <a href='%s'>Undo</a>",ARCHIVEPRESS), wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=activatefeed&amp;feed_id='.(int)$_GET["feed_id"], WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_postfeed_' . (int)$_GET["feed_id"]));
    } else {
        // BULK ACTIONS
        $message = sprintf(__("%d Feeds Deactivate",ARCHIVEPRESS), (int)$_GET["inactive"]);
    }
}

// update
if(array_key_exists("fetchedfeed", $_GET)){
    $feed = $ArchivePress->getFeed((int)$_GET["feed_id"]);
    $message = sprintf(__("Fetched feed %s (%s)> posts added: %d ",ARCHIVEPRESS), $ArchivePress->cleanOutput($feed["name"]), $ArchivePress->cleanOutput($feed["url"]), (int)$_GET["feeds"]);
}

// update comments
if(array_key_exists("fetchedcomments", $_GET)){
    $feed = $ArchivePress->getFeed((int)$_GET["feed_id"]);
    $message = sprintf(__("Fetched feed %s (%s)> comments added: %d ",ARCHIVEPRESS), $ArchivePress->cleanOutput($feed["name"]), $ArchivePress->cleanOutput($feed["url"]), (int)$_GET["feeds"]);
}

// reset
if(array_key_exists("resetfeed", $_GET)){
    if((int)$_GET["resetfeed"] == 1){
        // SINGLE ACTION
        $message = __("Feed Resetted", ARCHIVEPRESS);
    } else {
        // BULK ACTIONS
        $message = sprintf(__("%d Feeds Resetted",ARCHIVEPRESS), (int)$_GET["resetfeed"]);
    }

}

// delete
if(array_key_exists("deletefeed", $_GET)){
    if((int)$_GET["deletefeed"] == 1){
        // SINGLE ACTION
        $message = __("Feed Deleted", ARCHIVEPRESS);
    } else {
        // BULK ACTIONS
        $message = sprintf(__("%d Feeds Deleted",ARCHIVEPRESS), (int)$_GET["deletefeed"]);
    }
}

if($message != ""){
    $message = '<div class="updated fade below-h2" id="message">
                    <p>
                        '.$message.'<br />
                    </p>
                </div>';
}

// LOADING gif
echo '<div id="arpr_loadinggif"><img src="../wp-content/plugins/archivepress/images/ajax-loader.gif" alt="" /></div>';

echo '<div class="wrap">
        <div class="icon32" id="icon-archivepress"><br /></div>
        <h2>'.__("Edit Post Feeds", ARCHIVEPRESS).'
            <a class="button add-new-h2" href="admin.php?page=archivepress/pages/add-feed.php">'.__("Add New Feed", ARCHIVEPRESS).'</a>
        </h2>

        '.$message.'

        <form method="get" action="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=show_postfeeds', WP_ARCHIVEPRESS_PREFIX . 'show_postfeeds').'" id="posts-filter">
        <input type="hidden" name="page" value="archivepress/pages/responder.php" />
        <input type="hidden" name="'.WP_ARCHIVEPRESS_PREFIX.'action" value="show_postfeeds" />
        <input type="hidden" name="back" value="edit-posts" />
        <input type="hidden" name="_wpnonce" value="'.wp_create_nonce(WP_ARCHIVEPRESS_PREFIX . 'show_postfeeds').'" />';


// total and active feeds
$totFeeds = $ArchivePress->getBlogs();

if($status == "active"){
    echo '
        <ul class="subsubsub">
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/edit-posts.php">'.__("All", ARCHIVEPRESS).' <span class="count">('.$totFeeds["total"].')</span></a> |</li>
            <li><a class="current" href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/edit-posts.php&amp;post_status=active">'.__("Active", ARCHIVEPRESS).' <span class="count">('.$totFeeds["active"].')</span></a></li>
        </ul>';
} else {
    echo '
        <ul class="subsubsub">
            <li><a class="current" href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/edit-posts.php">'.__("All", ARCHIVEPRESS).' <span class="count">('.$totFeeds["total"].')</span></a> |</li>
            <li><a href="'.get_bloginfo( 'wpurl' ).'/wp-admin/admin.php?page=archivepress/pages/edit-posts.php&amp;post_status=active">'.__("Active", ARCHIVEPRESS).' <span class="count">('.$totFeeds["active"].')</span></a></li>
        </ul>';
}

// PAGINATION
$paging = "";
if(isset($_GET['paging'])){
    $paging = attribute_escape($_GET['paging']);
}
if($status == "active") {
    $items = count($ArchivePress->getFeeds(true, true, $search, $protocol));
}else {
    $items = count($ArchivePress->getFeeds(false, true, $search, $protocol));
}
$p = new pagination;
$p->items($items);
$p->limit(15); // Limit entries per page
$p->target(str_replace("&","&amp;",$_SERVER['REQUEST_URI']));
$p->currentPage($paging); // Gets and validates the current page
$p->calculate(); // Calculates what to show
$p->parameterName('paging');
$p->adjacents(2); //No. of page away from the current page

if($paging == "") {
    $p->page = 1;
} else {
    $p->page = $paging;
}

$p->prevT = "";
$p->nextT = "";
$p->displaying = __("Displaying", ARCHIVEPRESS);
$p->of = __("of", ARCHIVEPRESS);

// limit for query
$limit = " LIMIT " . ($p->page - 1) * $p->limit  . ", " . $p->limit;

echo'
        <p class="search-box">
            <label for="post-search-input" class="screen-reader-text">'.__("Search Feeds", ARCHIVEPRESS).':</label>
            <input type="text" value="'.$ArchivePress->cleanOutput($search).'" name="s" id="post-search-input" />
            <input type="submit" class="button" value="'.__("Search Feeds", ARCHIVEPRESS).'" />
        </p>

        <div class="tablenav">

            <div class="alignleft actions">
                <select name="action">
                    <option selected="selected" value="-1">'.__("Bulk Actions", ARCHIVEPRESS).'</option>
                    <option value="activatefeed">'.__("Activate", ARCHIVEPRESS).'</option>
                    <option value="deactivatefeed">'.__("Deactivate", ARCHIVEPRESS).'</option>
                    <option value="resetfeed">'.__("Reset", ARCHIVEPRESS).'</option>
                    <option value="deletefeed">'.__("Delete", ARCHIVEPRESS).'</option>
                </select>
                <input type="submit" class="button-secondary action" id="doaction" name="doaction" value="Apply" />
                <select class="postform" name="protocol">
                    <option value="ALL"'; if($protocol == "") echo ' selected="selected"'; echo'>'.__("View all protocols", ARCHIVEPRESS).'</option>
                    <option value="'.ArchivePressOptions::FEED_PROTOCOL_ATOM.'"'; if($protocol == ArchivePressOptions::FEED_PROTOCOL_ATOM) echo ' selected="selected"'; echo'>'.__("Atom", ARCHIVEPRESS).'</option>
                    <option value="'.ArchivePressOptions::FEED_PROTOCOL_RSS.'"'; if($protocol == ArchivePressOptions::FEED_PROTOCOL_RSS) echo ' selected="selected"'; echo'>'.__("Rss", ARCHIVEPRESS).'</option>
                </select>
                <input type="submit" class="button-secondary" value="Filter" id="post-query-submit" />
            </div>

            <div class="tablenav">
                <div class="tablenav-pages">
                    '.$p->getOutput().'
                </div>
            </div>

            <div class="clear"></div>
        </div>

        <div class="clear"></div>

        <table cellspacing="0" class="widefat post fixed">
            <thead>
                <tr>
                    <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox" /></th>
                    <th style="" class="manage-column column-arpr_name" id="name" scope="col">'.__("Name", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_url" id="url" scope="col">'.__("Feed Url", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_feedposts" id="feedposts" scope="col">'.__("Posts", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_feedauthors" id="feedauthors" scope="col">'.__("Authors", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_lastfetch" id="lastfetch" scope="col">'.__("Last fecth", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_nextfetch" id="nextfetch" scope="col">'.__("Next fetch", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_info" id="info" scope="col">'.__("Other Info", ARCHIVEPRESS).'</th>
                </tr>
            </thead>

            <tfoot>
                <tr>
                    <th style="" class="manage-column column-cb check-column" scope="col"><input type="checkbox" /></th>
                    <th style="" class="manage-column column-arpr_name" scope="col">'.__("Name", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_url" scope="col">'.__("Feed Url", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_feedposts" scope="col">'.__("Posts", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_feedauthors" scope="col">'.__("Authors", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_lastfetch" scope="col">'.__("Last fecth", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_nextfetch" scope="col">'.__("Next fetch", ARCHIVEPRESS).'</th>
                    <th style="" class="manage-column column-arpr_info" scope="col">'.__("Other Info", ARCHIVEPRESS).'</th>
                </tr>
            </tfoot>

            <tbody>';

// get feeds
$activeFeeds = array();
if($status == "active") {
    $activeFeeds = $ArchivePress->getFeeds(true, false, $search, $protocol, $limit);
}else {
    $activeFeeds = $ArchivePress->getFeeds(false, false, $search, $protocol, $limit);
}

// print feeds
if(count($activeFeeds) > 0){
    foreach($activeFeeds as $feed){
        // last and next fetching date and time
        $lastFetch = $ArchivePress->getLastFetchedForFeed($feed["id"]);
        $nextFetch = $ArchivePress->getNextToFetchForFeed($feed["id"]);
        // if the feed is not active, DON'T print the next fetching time
        // and activate or deactivate links
        if($feed["active"] == "yes"){
            $nextOutput = date('F jS, Y @ H:i',$nextFetch["datetime"]);
            $activate_deactivate = '<span class="edit"><a title="'.__("Deactivate this feed", ARCHIVEPRESS).'" href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=deactivatefeed&amp;feed_id='.$feed["id"], WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_postfeed_' . $feed["id"]).'">'.__("Deactivate", ARCHIVEPRESS).' </a> | </span>';
        } else {
            $nextOutput = __("Inactive", ARCHIVEPRESS);
            $activate_deactivate = '<span class="edit"><a title="'.__("Activate this feed", ARCHIVEPRESS).'" href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=activatefeed&amp;feed_id='.$feed["id"], WP_ARCHIVEPRESS_PREFIX . 'activate_deactivate_postfeed_' . $feed["id"]).'">'.__("Activate", ARCHIVEPRESS).' </a> | </span>';
        }

        // check if update also comments
        if($feed["comments"] == "yes"){
            $update_comments = '<span class="edit"><a title="'.__("Update comments for this feed", ARCHIVEPRESS).'" href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=updatefeedcomments&amp;feed_id='.$feed["id"], WP_ARCHIVEPRESS_PREFIX . 'updatefeedcomments_postfeed_' . $feed["id"]).'">'.__("Update comments", ARCHIVEPRESS).' </a> | </span>';
        } else {;
            $update_comments = '';
        }

        echo'
                <tr valign="top" class="alternate iedit" id="feed-'.$feed["id"].'">
                    <th class="check-column" scope="row"><input type="checkbox" value="'.$feed["id"].'" name="feed[]" /></th>
                    <td class="column-arpr_name"><strong><a title="'.__("Edit", ARCHIVEPRESS).' '.$ArchivePress->cleanOutput($feed["name"]).'" href="https://da.ulcc.ac.uk/archivepress/wp-admin/post.php?action=edit&amp;post=63" class="row-title">'.$ArchivePress->cleanOutput($feed["name"]).'</a></strong>
                        <div class="row-actions">
                            <span class="edit"><a title="'.__("Edit this feed", ARCHIVEPRESS).'" href="admin.php?page=archivepress/pages/edit-post.php&amp;feed_id='.$feed["id"].'">'.__("Edit", ARCHIVEPRESS).' </a> | </span>
                            '.$activate_deactivate.'
                            <span class="edit"><a title="'.__("Update this feed", ARCHIVEPRESS).'" href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=updatefeed&amp;feed_id='.$feed["id"], WP_ARCHIVEPRESS_PREFIX . 'update_postfeed_' . $feed["id"]).'">'.__("Update", ARCHIVEPRESS).' </a> | </span>
                            '.$update_comments.'
                            <span class="arpr_reset"><a title="'.__("Reset this feed", ARCHIVEPRESS).'" href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=resetfeed&amp;feed_id='.$feed["id"], WP_ARCHIVEPRESS_PREFIX . 'resetfeed_postfeed_' . $feed["id"]).'">'.__("Reset", ARCHIVEPRESS).' </a> | </span>
                            <span class="trash arpr_trash"><a href="'.wp_nonce_url($responder . '&amp;'.WP_ARCHIVEPRESS_PREFIX.'action=deletefeed&amp;feed_id='.$feed["id"], WP_ARCHIVEPRESS_PREFIX . 'deletefeed_postfeed_' . $feed["id"]).'" title="'.__("Delete this feed", ARCHIVEPRESS).'" class="submitdelete">Delete</a> | </span>
                            <span class="edit"><a title="'.__("Show metadata for this feed", ARCHIVEPRESS).'" href="admin.php?page=archivepress/pages/metadata-changelog.php&amp;feed_id='.$feed["id"].'">'.__("Show Metadata", ARCHIVEPRESS).' </a></span>
                       </div>
                    </td>
                    <td class="column-arpr_url"><a title="'.$ArchivePress->cleanOutput($feed["name"]).'" href="'.$ArchivePress->cleanOutput($feed["url"]).'">'.$ArchivePress->cleanOutput($feed["url"]).'</a></td>
                    <td class="column-arpr_feedposts">
                            <a title="'.$ArchivePress->cleanOutput($feed["name"]).' '.__("posts", ARCHIVEPRESS).'" href="'.get_bloginfo( 'wpurl' ).'/wp-admin/edit.php?s&amp;cat='.$ArchivePress->getCategoryForFeed($feed["id"]).'">
                                '.$ArchivePress->getPostsForFeed($feed["id"]).'
                            </a>
                    </td>
                    <td class="column-arpr_feedauthors">
                            <a title="'.$ArchivePress->cleanOutput($feed["name"]).' '.__("authors", ARCHIVEPRESS).'" href="'.get_bloginfo( 'wpurl' ).'/wp-admin/edit.php?s&amp;cat='.$ArchivePress->getCategoryForFeed($feed["id"]).'">
                                '.$ArchivePress->getAuthorsForFeed($feed["id"]).'
                            </a>
                    </td>
                    <td class="column-arpr_lastfetch">'.mysql2date('F jS, Y @ H:i',$lastFetch["datetime"]).'</td>
                    <td class="column-arpr_nextfetch">'.$nextOutput.'</td>
                    <td class="column-arpr_info">
                        '.__("Protocol", ARCHIVEPRESS).': '.$ArchivePress->cleanOutput($feed["protocol"]).'<br />
                        '.__("Comments", ARCHIVEPRESS).': '.$ArchivePress->cleanOutput($feed["comments"]).'<br />
                        '.__("Interval", ARCHIVEPRESS).': '.$ArchivePress->cleanOutput($feed["interval"]).' s<br /><br />
                        '.__("Permalinks", ARCHIVEPRESS).': '.$ArchivePress->cleanOutput($feed[WP_ARCHIVEPRESS_PREFIX . "followPermaLink"]).'<br />
                        '.__("Save Media", ARCHIVEPRESS).': '.$ArchivePress->cleanOutput($feed[WP_ARCHIVEPRESS_PREFIX . "embedMedia"]).'<br />
                        '.__("Excerpt", ARCHIVEPRESS).': '.$ArchivePress->cleanOutput($feed[WP_ARCHIVEPRESS_PREFIX . "tryToFindExcerpt"]).'
                    </td>
                </tr>';
    }
} else {
    echo '<tr valign="top" class="alternate iedit" id="feed-0"><td colspan="7">'.__("No Feed found", ARCHIVEPRESS).'</td></tr>';
}

echo'
            </tbody>
        </table>
        <div class="tablenav">

            <div class="tablenav">
                <div class="tablenav-pages">
                    '.$p->getOutput().'
                </div>
            </div>

            <div class="alignleft actions">
                <select name="action2">
                    <option selected="selected" value="-1">'.__("Bulk Actions", ARCHIVEPRESS).'</option>
                    <option value="activatefeed">'.__("Activate", ARCHIVEPRESS).'</option>
                    <option value="deactivatefeed">'.__("Deactivate", ARCHIVEPRESS).'</option>
                    <option value="resetfeed">'.__("Reset", ARCHIVEPRESS).'</option>
                    <option value="deletefeed">'.__("Delete", ARCHIVEPRESS).'</option>
                </select>
                <input type="submit" class="button-secondary action" id="doaction2" name="doaction2" value="Apply" />
                <br class="clear" />
            </div>
            <br class="clear" />
        </div>
    </form>
    </div>';

?>